package cn.bj.yu.pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 周宇
 * @create 2020-08-14 13:19
 * Executors 工具类、3大方法
 * 使用了线程池之后要使用线程池创建线程
 */
public class ExecutorDemo {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newSingleThreadExecutor();//单个线程
        //ExecutorService threadPool = Executors.newFixedThreadPool(5);//创建一个固定的线程池大小
        //ExecutorService threadPool = Executors.newCachedThreadPool();//可以伸缩的，遇强者强，遇弱则弱
        try {
            for (int i = 1; i <= 10; i++) {
                //使用线程池之后，使用线程池来创建线程
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + " ok!");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //线程池用完，程序结束，关闭线程池
            threadPool.shutdown();
        }
    }
}
